package com.thundersoft.musicbox;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;
import android.util.Log;
import com.thundersoft.musicbox.model.Music;

import java.io.InputStream;
import java.security.Key;
import java.util.ArrayList;
import java.util.List;

/**
 * 备注:
 *
 * @author hmb
 * @version 1.0
 * @since 2015/11/19 14:17
 */
public class MusicContext {
    private static final String TAG = "MusicContext";
    private static String[] keys = new String[]{
            MediaStore.Audio.Media._ID,
            MediaStore.Audio.Media.TITLE,
            MediaStore.Audio.Media.TITLE_KEY,
            MediaStore.Audio.Media.ARTIST,
            MediaStore.Audio.Media.ALBUM,
            MediaStore.Audio.Media.DISPLAY_NAME,
            MediaStore.Audio.Media.SIZE,
            MediaStore.Audio.Media.IS_MUSIC,
            MediaStore.Audio.Media.DATA,
            MediaStore.Audio.Media.DURATION

    };
    private static final List<Music> MUSIC_LIST = new ArrayList<Music>();
    private static boolean isFirst = true;

    public static synchronized List<Music> initMusicData(){
        Log.d(TAG,"加载歌曲列表");
        isFirst = false;

        ContentResolver contentResolver = MusicApplication.getContext().getContentResolver();

        Cursor cursor = contentResolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
                keys,
                MediaStore.Audio.Media.DURATION+">10000",
                null,
                MediaStore.Audio.Media.DEFAULT_SORT_ORDER);

        Log.d(TAG,"找到"+cursor.getCount()+"首音乐");
        if (cursor.moveToFirst()){
            List<Music> list = MUSIC_LIST;
            list.clear();
            while (cursor.moveToNext()){
                Music music = new Music();

                music.setTitle(getString(cursor, MediaStore.Audio.Media.TITLE));
                music.setArtist(getString(cursor, MediaStore.Audio.Media.ARTIST));
                music.setAlbum(getString(cursor, MediaStore.Audio.Media.ALBUM));
                music.setDisplayName(getString(cursor, MediaStore.Audio.Media.DISPLAY_NAME));
                music.setPath(getString(cursor, MediaStore.Audio.Media.DATA));
                music.setDuration(getInt(cursor, MediaStore.Audio.Media.DURATION));
                music.setSize(getLong(cursor, MediaStore.Audio.Media.SIZE));

                Log.d("MusicContext", "title=" + music.getTitle());
                list.add(music);
            }
        }else {
            Log.d(TAG,"没有音乐");
        }
        return MUSIC_LIST;
    }

    public static List<Music> getMusicList() {
        if (isFirst){
            synchronized (MUSIC_LIST) {
                initMusicData();
            }
        }
        return MUSIC_LIST;
    }


    private static String getString(Cursor cursor,String name){
        return cursor.getString(cursor.getColumnIndex(name));
    }
    private static int getInt(Cursor cursor,String name){
        return cursor.getInt(cursor.getColumnIndex(name));
    }
    private static long getLong(Cursor cursor,String name){
        return cursor.getLong(cursor.getColumnIndex(name));
    }
}
